package HomeWork.fraction;

/**
 * Enjoy programming!!!
 * Time: 2021/12/24 -  23:06
 */
public class Fraction_final{
    private int den;//分母
    private int mol;//分子

    public Fraction_final(int mol, int den)
    {
        this.mol = mol;
        this.den = den;
    }


    public void print()
    {
        boolean flag;
        do{
            flag = false;
            for(int i = 2; den >= i && mol >= i; i++){
                if (0 == den % i && 0 == mol % i) {
                    den /= i;
                    mol /= i;
                    flag = true;
                }
            }
        }
        while(flag);
        System.out.println(mol + "/" + den);
    }

    public Fraction_final plus(Fraction_final r)
    {
       int sum_den = r.den;
       int sum_mol = r.mol;
       int tden = den;
       int tmol = mol;
       int lcm = tden;//求最小公倍数least common multiple
       for (;;lcm++){
           if (0 == lcm % den && 0 == lcm % sum_den) {
               break;
           }
       }
       sum_mol *= lcm / sum_den;
       tmol *= lcm / tden;
        return new Fraction_final(sum_mol + tmol, lcm);
    }

    public Fraction_final multiply(Fraction_final r)
    {
        return new Fraction_final( mol * r.mol , den * r.den );
    }
}
